const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const goods = require('../../services/goods.js');

const app = getApp()
var canUseReachBottom = true;//触底函数控制变量
Page({
    data: {
        goodsList: [],
        page: 1,
        noMore: true,
        cate_id: 0,
        y: 0,
    },
    onLoad: function(options) {
        this.getGoodsList(options.id);
    },
    getGoodsList: function(cate_id) {
        let that = this;
        let page = this.data.page;
        goods.getGoods(cate_id, page).then(function (res) {
            if (res.code == 0) {
                that.setData({
                    goodsList: res.data,
                    cate_id: cate_id
                })
            }
        })
    },
    scrollToLower: function (e) {
        if(!canUseReachBottom) return;//如果触底函数不可用，则不调用网络请求数据
        let that = this;
        let cateId = that.data.cate_id;
        let page = that.data.page + 1;
        let no = that.data.noMore;
        if (this.data.noMore) {
            canUseReachBottom = false;//触底函数关闭
            goods.getGoods(cateId,page).then(function (res) {
                if (res.data.length > 0 && no) {
                    that.setData({
                        page: page,
                        goodsList: that.data.goodsList.concat(res.data)
                    })
                } else {
                    that.setData({
                        noMore: false
                    })
                }
                canUseReachBottom = true;//有新数据，触底函数开启，为下次触底调用做准备
            })
        }
    },
})